(function ($) {
	var sourceData;
	var perInfo,pageType,nurseid,perid
	var domArr = [{'domId':'EduStDate','tip':'请选择入学日期','type':'date','key':'EduStDate'},
			{'domId':'EduEndDate','tip':'','type':'date','key':'EduEndDate'},
			{'domId':'EduRadio','tip':'请选择教育性质','type':'list','key':'EduRadio'},
			{'domId':'EduSchool','tip':'请选择毕业院校','type':'list','key':'EduSchool'},
			{'domId':'EduProfession','tip':'','type':'list','key':'EduProfession'},
			{'domId':'EduAcademic','tip':'请选择学历','type':'list','key':'EduAcademic'},
			{'domId':'EduDegree','tip':'','type':'list','key':'EduDegree'},
			{'domId':'EduDegreeDate','tip':'','type':'date','key':'EduDegreeDate'}]
	var unUploadFiles=[] //未上传的图片
	$.init();
	$.plusReady(function() {
		baseSetup();
	})
	if (!mui.os.plus) {
		baseSetup()
	}
	//EduStDate|2000-02-02^EduEndDate|^EduRadio|23||1^EduSchool|18||1^EduProfession|^EduAcademic|3||1^EduDegree|15||1^EduDegreeDate|^RowID|2^EduPerDR|2
	function submitNewData(){
		blurAllInput()
		var parr = ''
		for (var i = 0; i < domArr.length; i++) {
			var oneDom = domArr[i]
			var dom = $('#'+oneDom['domId'])[0]
			var domValue = dom.getAttribute('data-id')
			domValue = (domValue == undefined?"":domValue)
			if (oneDom['type'] != 'list') {
				domValue = dom.innerText
			}
			if (domValue == '' && oneDom['tip']!= '') {
				mui.toast(oneDom['tip'])
				return
			}
			parr += oneDom['key']+'|'+domValue+'^'
		}
		if (sourceData) {
			parr += 'RowID|'+sourceData['eduRowID']+'^'
		}else{
			parr += 'RowID|^'
		}
		parr += 'EduPerDR|'+perid
		var status = this.getAttribute('data-id');
		//console.log(parr)
		var pdict = {'parr':parr,'type':status}
		var paramDict = {'method':'web.INMPersonComm:SaveEducate','params':JSON.stringify(pdict),'type':'Method'}
		$.DHCWebService(paramDict, function(result) {
			//console.log(JSON.stringify(result))
			var msgcode =result['msgcode']
			if (msgcode == '-1') {
				mui.toast(result['msg'])
				return 
			}
			
			if (unUploadFiles.length==0) {
				plus.webview.getWebviewById('../WKPlatformPages/apply/applyList.html').evalJS('detailChanged()')
				mui.back()
				return;
			}
			var path = 'cert'
			var method = 'web.INMPersonComm:InsertPic'
			var paramDict={'id':perid,'path':path,'imgArr':unUploadFiles,'method':method,'otherParams':result['msg']+'||1||','spStr':'^'}
			saveFileAction(paramDict,function(flag){
				if (!flag) {
					return
				}
				unUploadFiles=[]
				plus.webview.getWebviewById('../WKPlatformPages/apply/applyList.html').evalJS('detailChanged()')
				mui.back()
			})
		}, function(errorStr) {
			mui.toast(errorStr)
		});
	}
	
	function getTransDetail(){
		var pdict = {'id':sourceData['eduRowID']};
		//console.log(id + ' ' + method)
		var paramDict = {'method':'web.INMPersonComm:GetEduItem','params':JSON.stringify(pdict),'type':'RecMethod'}
		$.DHCWebService(paramDict, function(result) {
			//console.log(JSON.stringify(result))
			var dataDict = result.data
			updateUI(dataDict)
		}, function(errorStr) {
		 	
		});
	}
	
	function updateUI(dataDict){
		for (var i = 0; i < domArr.length; i++) {
			var oneDom = domArr[i]
			var dom = $('#'+oneDom['domId'])[0]
			dom.innerText = dataDict[oneDom['key']]
			if (oneDom['type'] == 'list') {
				dom.setAttribute('data-id',dataDict[oneDom['key']])
				dom.innerText = dataDict[oneDom['key']+'Desc']
			}
		}
		updateImageFileUI('eduFile','fileUL',dataDict,dataDict['RowID'])
	}

	function baseSetup(){
		$('.mui-scroll-wrapper').scroll({
			bounce: true,
			indicators: false, //是否显示滚动条
		});
		mui.previewImage();
		var self = plus.webview.currentWebview()
		sourceData = self.data
		pageType = self.pageType
		if (sourceData) { //修改
			getTransDetail()
		}
		perInfo = self.perInfo
		nurseid = perInfo['loginID']
		perid = perInfo['loginPerID']
		$('#CreatorName')[0].innerText = perInfo['loginName']
		$('#applyLoc')[0].innerText = perInfo['loginWardDesc']
		//关闭按钮
		mui('body').on('tap','#closeBtn',function(){
			blurAllInput()
			mui.back()
		})
		//保存提交按钮
		mui('body').on('tap','.submitBtn',submitNewData)
		// 日期
		mui('#UL').on('tap','.dateType',selectDate);
		//层级
		mui('#UL').on('tap','.listType',downListElementTapped);
		window.addEventListener('resize',windowResize)
		$('#addFileBtn').on('change','input',function(){
			var imgArr = this.files
			var name = imgArr[0]['name']
			if (fileTypeIcon(name) != 'img') {
				mui.toast('仅支持图片哦~~')
				return;
			}
			unUploadFiles = afterSelectImageFile('fileUL',unUploadFiles,imgArr)
		});
		//监听文件删除按钮
		mui('#fileUL').on('tap','.mui-icon-closeempty',function(){
			var method = 'web.INMPersonComm:DeleteCert'
			var otherParams = '||1||'
			var paramDict={'imgArr':unUploadFiles,'method':method,'nurseid':nurseid,'otherParams':otherParams}
			deleteFileBtnTapped(this,paramDict,function(flag,arr){
				if (arr) {
					unUploadFiles = arr
				}
			});
		}) 
		mui.back = function(){
			if (unUploadFiles.length > 0) {
				mui.confirm('您有未上传的文件','温馨提示',['放弃','继续操作'],function (e) {
					if (e.index == 0) {
						plus.nativeUI.closeWaiting()
						plus.webview.currentWebview().close()
					}
				},'div')
				return
			}
			plus.nativeUI.closeWaiting()
			plus.webview.currentWebview().close()
		}
	}
	
})(mui)

